/*
 * AIVariationResource.cpp
 *
 *  Created on: 2013. 9. 10.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "AIVariationResource.h"
#include <Database/Static.h>
#include "../NUtil/Utils.h"

namespace ProjectZ { namespace NResource {

AIVariationResource::AIVariationResource()
{
	pAIVariationAD	= new AIVariationData;
	pAIVariationIOS	= new AIVariationData;
}

AIVariationResource::~AIVariationResource()
{
	delete pAIVariationAD;
	delete pAIVariationIOS;
}

bool AIVariationResource::LoadResource()
{
	if (false == pAIVariationAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pAIVariationIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

AIVariationData::Flyweight * AIVariationResource::GetFly(User * pUser, int level, bool bPvP)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetUserInfo().company, pUser->GetUserInfo().sale_code);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		if (true == bPvP)
		{
			return pAIVariationAD->GetPvPFly(level);
		}

		return pAIVariationAD->GetPkFly(level);

	case NUtil::MarketType::IOS_KAKAO:
		if (true == bPvP)
		{
			return pAIVariationIOS->GetPvPFly(level);
		}

		return pAIVariationIOS->GetPkFly(level);

	default:
		return NULL;
	}

	return NULL;
}


AIVariationData::~AIVariationData()
{
	std::for_each(clsPvPFlyTree.begin(), clsPvPFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	std::for_each(clsPkFlyTree.begin(), clsPkFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
AIVariationData::Clear()
{
	std::for_each(clsPvPFlyTree.begin(), clsPvPFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsPvPFlyTree.clear();

	std::for_each(clsPkFlyTree.begin(), clsPkFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsPkFlyTree.clear();
}

bool AIVariationData::LoadResource(int company, int saleCode)
{
	Clear();

	// PvP HP 보정 데이터 Load
	{
		::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
		clsAuto->SetQueryType(::Database::Command::SELECT);
		clsAuto->MakeQuery("SELECT * FROM resource_pvp_hp_adjust WHERE company=%d AND sale_cd=%d", company, saleCode);

		if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
			return false;

		if( clsAuto->GetErrorIdx() != 0 )
			return false;

		if (0 == clsAuto->ResultRowCount())
			return false;

		while( clsAuto->Next() )
		{
			::Toolkit::Str<3> NATIONAL_CODE;
			int seq;
			int tmp_company;
			int tmp_salecode;

			AIVariationData::Flyweight * pFly = new AIVariationData::Flyweight;

			// 성기사 클래스 추가, Changed by spencer(2014.07.02)			
			clsAuto->GetRecord() >> NATIONAL_CODE >> seq >> tmp_company >> tmp_salecode >> pFly->_MIN_LEVEL >> pFly->_MAX_LEVEL
					>> pFly->_SLASHER >> pFly->_RANGER >> pFly->_FIGHTER >> pFly->_MAGICIAN >> pFly->_ASSASSIN >> pFly->_PALADIN;

			if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
			{
				DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
						__FILE__, __LINE__,
						Config::Instance().GetResourceDatabase()._strNational.c_str(),
						NATIONAL_CODE.c_str());
				return false;
			}

			clsPvPFlyTree[pFly->_MIN_LEVEL] = pFly;
		}
	}


	// 배틀로얄 HP 보정 데이터 Load
	{
		::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
		clsAuto->SetQueryType(::Database::Command::SELECT);
		clsAuto->MakeQuery("SELECT * FROM resource_pk_hp_adjust WHERE company=%d AND sale_cd=%d", company, saleCode);

		if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
			return false;

		if( clsAuto->GetErrorIdx() != 0 )
			return false;

		if (0 == clsAuto->ResultRowCount())
			return false;

		while( clsAuto->Next() )
		{
			::Toolkit::Str<3> NATIONAL_CODE;
			int seq;
			int tmp_company;
			int tmp_salecode;

			AIVariationData::Flyweight * pFly = new AIVariationData::Flyweight;

			clsAuto->GetRecord() >> NATIONAL_CODE >> seq >> tmp_company >> tmp_salecode >> pFly->_MIN_LEVEL >> pFly->_MAX_LEVEL
					>> pFly->_SLASHER >> pFly->_RANGER >> pFly->_FIGHTER >> pFly->_MAGICIAN >> pFly->_ASSASSIN >> pFly->_PALADIN;

			if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
			{
				DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
						__FILE__, __LINE__,
						Config::Instance().GetResourceDatabase()._strNational.c_str(),
						NATIONAL_CODE.c_str());
				return false;
			}

			clsPkFlyTree[pFly->_MIN_LEVEL] = pFly;
		}
	}

	return true;
}

AIVariationData::Flyweight * AIVariationData::GetPvPFly(int level)
{
	int levelRange = 1;

	if (level >= 10)
		levelRange = (level / 10) * 10;

	auto iter = clsPvPFlyTree.find(levelRange);

	if (iter == clsPvPFlyTree.end())
		return NULL;

	return iter->second;
}

AIVariationData::Flyweight * AIVariationData::GetPkFly(int level)
{
	int levelRange = 1;

	if (level >= 10)
		levelRange = (level / 10) * 10;

	auto iter = clsPkFlyTree.find(levelRange);

	if (iter == clsPkFlyTree.end())
		return NULL;

	return iter->second;
}

} /*NResource*/ } /* namespace ProjectZ */
